package jkd.lrn.net.ams.core.codec;

import jkd.lrn.net.ams.core.message.MDRResponse;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoder;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;

public class MDRResponseDecoder implements MessageDecoder {

	@Override
	public MessageDecoderResult decodable(IoSession session, IoBuffer buffer) {
		
		if(buffer.remaining() < Constants.MDR_RES_SIZE){
			return MessageDecoderResult.NEED_DATA;
		}else if(buffer.remaining() == Constants.MDR_RES_SIZE){
			return MessageDecoderResult.OK;
		}
		
		return MessageDecoderResult.NOT_OK;
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer buffer, ProtocolDecoderOutput out) throws Exception {
		
		if(buffer.remaining() < Constants.MDR_RES_SIZE){
			
			return MessageDecoderResult.NEED_DATA;
			
		}else {
			
			final MDRResponse response = new MDRResponse();
			response.setMessageId(buffer.getInt());
			response.setStatus(buffer.get());
			
			return MessageDecoderResult.OK;
		}

	}

	@Override
	public void finishDecode(IoSession session, ProtocolDecoderOutput out)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
